/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.fsiebert.polytech.db.mediatheque.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author Florent
 */
@Entity
@Table(name = "Exemplaire")
@NamedQueries({
    @NamedQuery(name = "Exemplaire.findAll", query = "SELECT e FROM Exemplaire e"),
    @NamedQuery(name = "Exemplaire.findByNumeroArticle", query = "SELECT e FROM Exemplaire e WHERE e.numeroArticle = :numeroArticle"),
    @NamedQuery(name = "Exemplaire.findByEtat", query = "SELECT e FROM Exemplaire e WHERE e.etat = :etat"),
    @NamedQuery(name = "Exemplaire.findAvailableByItem", query = "SELECT e FROM Exemplaire e WHERE e.itemRef = :itemRef AND NOT EXISTS (SELECT p FROM Emprunt p WHERE e member of p.exemplaireList) AND NOT EXISTS (SELECT r FROM ExemplaireReservation r WHERE e = r.exemplaire)")
})
public class Exemplaire implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "NumeroArticle", nullable = false)
    private Integer numeroArticle;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "Etat", nullable = false, length = 10)
    private String etat;
    @ManyToMany(mappedBy = "exemplaireList")
    private List<Emprunt> empruntList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "exemplaire")
    private List<ExemplaireReservation> exemplaireReservationList;
    @JoinColumn(name = "ItemRef", referencedColumnName = "ItemRef")
    @ManyToOne
    private Item itemRef;

    public Exemplaire() {
    }

    public Exemplaire(Integer numeroArticle) {
        this.numeroArticle = numeroArticle;
    }

    public Exemplaire(Integer numeroArticle, String etat) {
        this.numeroArticle = numeroArticle;
        this.etat = etat;
    }

    public Integer getNumeroArticle() {
        return numeroArticle;
    }

    public void setNumeroArticle(Integer numeroArticle) {
        this.numeroArticle = numeroArticle;
    }

    public String getEtat() {
        return etat;
    }

    public void setEtat(String etat) {
        this.etat = etat;
    }

    public List<Emprunt> getEmpruntList() {
        return empruntList;
    }

    public void setEmpruntList(List<Emprunt> empruntList) {
        this.empruntList = empruntList;
    }

    public List<ExemplaireReservation> getExemplaireReservationList() {
        return exemplaireReservationList;
    }

    public void setExemplaireReservationList(List<ExemplaireReservation> exemplaireReservationList) {
        this.exemplaireReservationList = exemplaireReservationList;
    }

    public Item getItemRef() {
        return itemRef;
    }

    public void setItemRef(Item itemRef) {
        this.itemRef = itemRef;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (numeroArticle != null ? numeroArticle.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Exemplaire)) {
            return false;
        }
        Exemplaire other = (Exemplaire) object;
        if ((this.numeroArticle == null && other.numeroArticle != null) || (this.numeroArticle != null && !this.numeroArticle.equals(other.numeroArticle))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "eu.fsiebert.polytech.db.mediatheque.entity.Exemplaire[ numeroArticle=" + numeroArticle + " ]";
    }
    
}
